package rhc.singleNodeSpider.execute;

import rhc.singleNodeSpider.spiderManage.SpiderManage;
import rhc.taskSchedule.entity.TaskType;
import rhc.taskSchedule.taskSignalCall.CustomCall;
import rhc.taskSchedule.taskSignalCall.NoticeCustom;
import rhc.taskSchedule.taskSignalCall.RegisterCallCustom;

/**在任务调度器注册的通知
 * @author rhc
 *
 */
public class SpiderTaskNoticeCustom implements NoticeCustom {

	/**
	 * 单节点爬虫管理器，单接收到通知时通过该管理器管理单节点爬虫
	 */
	private SpiderManage spiderManage;
	
	private CustomCallTaskScheduleManage customCallTaskScheduleManage;
	
	/**
	 * 执行器
	 */
	private SingleNodeSpiderExecuter singleNodeSpiderExecuter;
	
	public SpiderTaskNoticeCustom(SingleNodeSpiderExecuter singleNodeSpiderExecuter){
		this.singleNodeSpiderExecuter=singleNodeSpiderExecuter;
		spiderManage=singleNodeSpiderExecuter.getSpiderManage();
		customCallTaskScheduleManage=singleNodeSpiderExecuter.getCustomCallTaskScheduleManage();
	}
	
	@Override
	public void noticeCustom(String runningId, String taskId, TaskType taskType, CustomCall customCall,
			RegisterCallCustom retisterCallCustom) throws Exception {
		
		if(spiderManage.containsTask(taskId)){
			throw new IllegalArgumentException(taskId+" 已在运行");
		}
		//注册回调到调度器
		TaskScheduleCallSpider callSpider=new TaskScheduleCallSpider(spiderManage,runningId,taskId);
		retisterCallCustom.registerCallCustom(runningId, callSpider);
		
		customCallTaskScheduleManage.registerTaskCustomCall(taskId, customCall);
	}

}
